<?php
/**
 * 卡密导出控制器
 * @link https://www.leadshop.vip/
 * @copyright Copyright ©2020-2021 浙江禾成云计算有限公司
 */

namespace card\api;

use card\models\Card;
use framework\common\BasicController;
use Yii;
use yii\data\ActiveDataProvider;

class ExportController extends BasicController
{
    public $modelClass = 'card\models\CardExport';

    public function actions()
    {
        $actions = parent::actions();
        unset($actions['create']);
        unset($actions['update']);
        return $actions;
    }

    public function actionIndex()
    {
        //获取头部信息
        $headers = Yii::$app->getRequest()->getHeaders();
        //获取分页信息
        $pageSize = $headers->get('X-Pagination-Per-Page') ?? 20;

        $AppID = Yii::$app->params['AppID'];
        $where = [
            'is_deleted' => 0,
            'AppID' => $AppID,
        ];

        $data = new ActiveDataProvider(
            [
                'query' => $this->modelClass::find()->where($where)->orderBy(['created_time' => SORT_DESC])->asArray(),
                'pagination' => ['pageSize' => $pageSize, 'validatePage' => false],
            ]
        );

        $list = $data->getModels();
        foreach ($list as $key => &$value) {
            $value['conditions'] = to_array($value['conditions']);
            $value['card_data'] = to_array($value['card_data']);
        }
        $data->setModels($list);
        return $data;
    }

    public function actionCreate()
    {

        $keyword = Yii::$app->request->post('conditions', []); //查询条件
        $id_list = Yii::$app->request->post('id_list', []); //选择的商品
        $AppID = Yii::$app->params['AppID'];

        if (empty($id_list)) {
            $where = ['card.AppID' => $AppID, 'card.is_deleted' => 0];

            //卡密批次ID
            $card_batch_id = $keyword['card_batch_id'] ?? '';
            if ($card_batch_id) {
                $where = ['and', $where, ['=', 'card.card_batch_id', $card_batch_id]];
            }

            //卡号
            $number = $keyword['number'] ?? '';
            if ($number) {
                $where = ['and', $where, ['like', 'card.number', $number]];
            }

            //是否绑定
            $is_bind = $keyword['is_bind'] ?? '';
            if ($is_bind !== '') {
                $where = ['and', $where, ['=', 'card.is_bind', $is_bind]];
            }

            //用户昵称
            $nickname = $keyword['nickname'] ?? '';
            if ($nickname) {
                $where = ['and', $where, ['like', 'user.nickname', $nickname]];
            }

            //用户手机
            $mobile = $keyword['mobile'] ?? '';
            if ($mobile) {
                $where = ['and', $where, ['like', 'user.mobile', $mobile]];
            }

            //时间区间
            $expire_time_start = $keyword['expire_time_start'] ?? false;
            if ($expire_time_start > 0) {
                $where = ['and', $where, ['>=', 'card.begin_time', $expire_time_start]];
            }
            $expire_time_end = $keyword['expire_time_end'] ?? false;
            if ($expire_time_end > 0) {
                $where = ['and', $where, ['<=', 'card.end_time', $expire_time_end]];
            }

        } else {
            $where = ['user.id' => $id_list];
        }

        $data = Card::find()
            ->alias('card')
            ->joinWith([
                'user as user',
            ])
            ->where($where)
            ->asArray()
            ->all();

        $tHeader = [];
        $filterVal = [];
        $filter_list = [['name' => 'ID', 'value' => 'id'], ['name' => '卡号', 'value' => 'number'], ['name' => '卡密', 'value' => 'password']];
        foreach ($filter_list as $v) {
            array_push($tHeader, $v['name']);
            array_push($filterVal, $v['value']);
        }

        $list = [];
        foreach ($data as $value) {
            $res = $this->listBuild($value, $filterVal);
            array_push($list, $res);
        }

        $card_data = [
            'tHeader' => $tHeader,
            'list' => $list,
        ];

        $ins_data = [
            'conditions' => to_json($keyword),
            'card_data' => to_json($card_data),
            'AppID' => $AppID,
        ];
        $model = new $this->modelClass;
        $model->setAttributes($ins_data);
        if ($model->save()) {
            return $card_data;
        } else {
            Error('保存失败');
        }

    }

    /**
     * 导出字段筛选
     * @param  [type] $data      [description]
     * @param  [type] $filterVal [description]
     * @return [type]            [description]
     */
    public function listBuild($data, $filterVal)
    {
        $return_data = [];
        foreach ($filterVal as $key) {
            $value = '';
            switch ($key) {
                case 'id':
                    $value = $data['id'];
                    break;
                case 'number':
                    $value = $data['number'];
                    break;
                case 'password':
                    $value = $data['password'];
                    break;
            }

            array_push($return_data, $value);
        }

        return $return_data;

    }
}
